#include <Arduino.h>

#include "Wire.h"
#include "SHT31.h"

#define SHT31_ADDRESS 0x44

#define U1_RX_PIN 33 // 替换为您的接收引脚
#define U1_TX_PIN 32 // 替换为您的发送引脚

#define U2_RX_PIN 17 // 替换为您的接收引脚
#define U2_TX_PIN 16 // 替换为您的发送引脚


float home_temp, home_humi;
/*--------------------------------------------------*/
/*---------------------- Tasks ---------------------*/
/*--------------------------------------------------*/

void printHeaterStatus(uint16_t status)
{
  Serial.print(millis());
  Serial.print("\tHEATER: ");
  if (status & SHT31_STATUS_HEATER_ON)
  {
    Serial.println("ON");
  }
  else
  {
    Serial.println("OFF");
  }
}

void Task_data_query(void *pvParameters)
{ // This is a task.
  SHT31 sht(SHT31_ADDRESS);
  uint16_t status;

  Wire.begin();
  Wire.setClock(100000);
  sht.begin();

  sht.setHeatTimeout(10); //  heater timeout 10 seconds, just for demo.

  status = sht.readStatus();
  printHeaterStatus(status);

  sht.heatOn();

  while (sht.isHeaterOn())
  {
    status = sht.readStatus();
    printHeaterStatus(status);
    sht.read();
    Serial.println(sht.getTemperature());
    delay(10000);
  }

  Serial.println("switched off");

  for (;;)
  { // A Task shall never return or exit.

    sht.read();
    home_temp = sht.getTemperature();
    home_humi = sht.getHumidity();

    Serial.printf("temp=%.3f, humi=%.3f\r\n", home_temp, home_humi);

    delay(10000);

  } // Infinite loop
}

const char* P_TEMP_FORMAT_STR = "t0.txt=\"%5.2f\"";
const char* P_HUMI_FORMAT_STR = "t1.txt=\"%5.2f\"";

const char HMI_END_FRAME[]={0XFF,0XFF,0XFF,0x00};

void Task_HMI_gui(void *pvParameters)
{                                                        // This is a task.
  Serial1.begin(9600, SERIAL_8N1, U1_RX_PIN, U1_TX_PIN); // 初始化UART2
  for (;;)
  {
    delay(1000); //
    Serial1.printf(P_TEMP_FORMAT_STR,home_temp);
    Serial1.print(HMI_END_FRAME);
    delay(500); //
    Serial1.printf(P_HUMI_FORMAT_STR,home_humi);
    Serial1.print(HMI_END_FRAME);

  } // Infinite loop
}

void setup()
{
  // put your setup code here, to run once:

  Serial.begin(115200);
  Serial.println("Snail home is initialized");

  // Set up two tasks to run independently.
  xTaskCreate(
      Task_data_query, "Task Write To Serial" // A name just for humans
      ,
      2048 // The stack size can be checked by calling `uxHighWaterMark = uxTaskGetStackHighWaterMark(NULL);`
      ,
      NULL // No parameter is used
      ,
      2 // Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest.
      ,
      NULL // Task handle is not used here
  );

  xTaskCreate(
      Task_HMI_gui

      ,
      "Task Read From Serial", 2048 // Stack size
      ,
      NULL // No parameter is used
      ,
      1 // Priority
      ,
      NULL // Task handle is not used here
  );
}

void loop()
{
  // put your main code here, to run repeatedly:
  delay(1000*1000); // While not being used yield the CPU to other tasks
}

// #include "Wire.h"
// #include "SHT31.h"

// #define SHT31_ADDRESS   0x44

// SHT31 sht(SHT31_ADDRESS);
// uint16_t status;

// void setup()
// {
//   Serial.begin(115200);
//   Serial.println(__FILE__);
//   Serial.print("SHT31_LIB_VERSION: \t");
//   Serial.println(SHT31_LIB_VERSION);

//   Wire.begin();
//   Wire.setClock(100000);
//   sht.begin();

//   sht.setHeatTimeout(30);  //  heater timeout 30 seconds, just for demo.

//   status = sht.readStatus();
//   printHeaterStatus(status);

//   sht.heatOn();

//   while (sht.isHeaterOn())
//   {
//     status = sht.readStatus();
//     printHeaterStatus(status);
//     sht.read();
//     Serial.println(sht.getTemperature());
//     delay(10000);
//   }

//   Serial.println("switched off");
// }

// void loop()
// {
//   //  forced switch off
//   if (status & SHT31_STATUS_HEATER_ON) sht.heatOff();
// }

// void printHeaterStatus(uint16_t status)
// {
//   Serial.print(millis());
//   Serial.print("\tHEATER: ");
//   if (status & SHT31_STATUS_HEATER_ON)
//   {
//     Serial.println("ON");
//   } else {
//     Serial.println("OFF");
//   }
// }
